﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Meqs.Entity;
using Meqs.Logic.Interface;
using Castle.Services.Transaction;
using Castle.Facilities.IBatisNetIntegration;
using log4net;

namespace Meqs.Logic.Implement
{
    public class ExamsSchoolLogic : CommonLogic<ExamsSchool, int>, IExamsSchoolLogic
    {
        /// <summary>
        /// 查找学生参加的考试
        /// </summary>
        /// <param name="ht"></param>
        /// <returns></returns>
        public IList<ExamsSchool> FindStudentExam(Hashtable ht)
        {
            try
            {
                string statementName = "ExamsSchool.FindStuExam";
                return ExecuteQueryForList<ExamsSchool>(statementName, ht);
            }
            catch (Exception ex)
            {
                ILog _log = LogManager.GetLogger("DatabaseOp");
                _log.Error("查询全部对象出错：" + ex.Message, ex);
                throw;
            }
            return null;
        }

        #region Implementation of ICommonLogic<ExamsSchool, int>

        /// <sammury>
        /// 创建对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Create(ExamsSchool entity)
        {
            return CreateObject(entity);
        }

        /// <sammury>
        /// 更新对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(ExamsSchool entity)
        {
            return UpdateObject(entity);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public int Delete(int entityId)
        {
            return DeleteObject(entityId);
        }

        /// <summary>
        /// 删除所有对象
        /// </summary>
        /// <returns></returns>
        public int DeleteAll()
        {
            return DeleteAllObject();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public ExamsSchool FindById(int entityId)
        {
            return FindObjectById(entityId);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount()
        {
            return FindObjectByCount();
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<ExamsSchool> FindAll()
        {
            return FindAllObject();
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount(Hashtable table)
        {
            return FindObjectByCount(table);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<ExamsSchool> FindAll(Hashtable table)
        {
            return FindObject(table);
        }

        #endregion


        private readonly IExamSubjecySchoolLogic schoolExamSubjectLogic;
        private readonly IGlobalCauseInfoLogic globalCauseLogic;

        public ExamsSchoolLogic(IExamSubjecySchoolLogic _schoolExamSubjectLogic, IGlobalCauseInfoLogic _globalCauseLogic)
        {
            schoolExamSubjectLogic = _schoolExamSubjectLogic;
            globalCauseLogic = _globalCauseLogic;
        }
        /// <summary>
        /// 创建学校考试，以及考试对应科目信息
        /// </summary>
        /// <param name="eduExam"></param>
        /// <param name="listSubject"></param>
        /// <returns></returns>
        [Transaction(TransactionMode.Requires)]
        public int InsertShoolExam(ExamsSchool shoolExam, List<CauseInfo> listSubject)
        {
            int exameId = Create(shoolExam);
            if (exameId != -1)
            {
                foreach (CauseInfo item in listSubject)
                {
                    ExamSubjecySchool entity = new ExamSubjecySchool();
                    entity.ExamID = exameId;
                    entity.SubjectID = item.RID;
                    entity.SchoolMark = item.SchoolMark;
                    schoolExamSubjectLogic.Create(entity);
                }
            }
            return exameId;
        }


        /// <summary>
        /// 删除学校考试及其科目信息
        /// </summary>
        /// <param name="examId"></param>
        /// <returns></returns>
        [Transaction(TransactionMode.Requires)]
        public int DelShoolExam(int examId, string schoolMark)
        {
            Hashtable table = new Hashtable();
            table.Add("ExamId", examId);
            table.Add("SchoolMark", schoolMark);
            DeleteCustomer("ExamSubjecySchool.DeleteByExamId", table);
            table = new Hashtable();
            table.Add("RID", examId);
            table.Add("SchoolMark", schoolMark);
            DeleteCustomer("ExamsSchool.DeleteById", table);
            return 1;
        }
        /// <summary>
        /// 修改学校考试，及其科目信息
        /// </summary>
        /// <param name="eduExam"></param>
        /// <param name="listSubject"></param>
        /// <returns></returns>
        [Transaction(TransactionMode.Requires)]
        public int UpdateSchoolExam(ExamsSchool schoolExam, List<CauseInfo> listSubject)
        {
            Update(schoolExam);
            Hashtable table = new Hashtable();
            table.Add("ExamId", schoolExam.RID);
            table.Add("SchoolMark", schoolExam.SchoolMark);
            DeleteCustomer("ExamSubjecySchool.DeleteByExamId", table);
            foreach (CauseInfo item in listSubject)
            {
                ExamSubjecySchool entity = new ExamSubjecySchool();
                entity.SubjectID = item.RID;
                entity.SchoolMark = schoolExam.SchoolMark;
                entity.ExamID = schoolExam.RID;
                schoolExamSubjectLogic.Create(entity);
            } 
            return 1;
        }

       
    }
}